package com.rockxin.cat.base;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.orhanobut.logger.Logger;

import org.simple.eventbus.EventBus;

import java.lang.reflect.Field;

/**
 * <b>类名称：    BaseFragment             <br/>
 * <b>类描述：                        <br/>
 * <b>创建人：    Lans               <br/>
 * <b>创建时间：  2018/3/1 10:00     <br/>
 * <b>修改人：                        <br/>
 * <b>修改时间：                      <br/>
 * <b>修改备注：                      <br/>
 *
 * @version 1.0.0 <br/>
 */
public class BaseFragment extends Fragment {

    public Context mContext;

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }


    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.mContext = context;
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.e(getClass().getSimpleName() , "-->onResume");
    }

    @Override
    public void onStop() {
        super.onStop();
        Log.e(getClass().getSimpleName() , "-->onStop");
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(getClass().getSimpleName() , "-->onDestroy");
        EventBus.getDefault().register(this);
    }

    /**
     * 防止fragment重叠
     */
    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class
                    .getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this, null);

        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 显示隐藏fragment的log显示
     *
     * @param hidden 隐藏
     */
    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (hidden) {
            Log.e(this.getClass().getSimpleName(), "hidden");
        } else {
            Log.e(this.getClass().getSimpleName(), "show");
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            Log.e(this.getClass().getSimpleName(), "UserVisibleHint--->show");
        } else {
            Log.e(this.getClass().getSimpleName(), "UserVisibleHint--->hidden");
        }
    }
}
